/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.*;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class StackTraceElement extends ModelObject {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private ThreadInformation thread;
    @Persistent
    private int index;
    @Persistent
    private Key sourceLocationId;
    @NotPersistent
    private SourceLocation sourceLocation;

    public StackTraceElement() {
    }

    public StackTraceElement(int index, SourceLocation sourceLocation) {
        this.index = index;
        setSourceLocation(sourceLocation);
    }

    public Key getId() {
        return id;
    }

    public ThreadInformation getThread() {
        return thread;
    }

    public void setThread(ThreadInformation thread) {
        this.thread = thread;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public SourceLocation getSourceLocation() {
        if (sourceLocation == null) {
            if (sourceLocationId == null) {
                return null;
            }
            sourceLocation = DB.findEx(SourceLocation.class, sourceLocationId);
        }
        return sourceLocation;
    }

    public void setSourceLocation(SourceLocation sourceLocation) {
        this.sourceLocation = sourceLocation;
        this.sourceLocationId = sourceLocation.getId();
    }
}
